#pragma once
////AO 14 AI 15 
////DO 48 DI 49 
#include<vector>
#include <pthread.h>
unsigned char XORCheck(unsigned char* pBuffer, unsigned int iLength)
{
    unsigned char b = 0;
    for (int i = 0; i < iLength; i++)
    {
        b ^= pBuffer[i];
    }
    return b;
}
enum
{
    AO_TYPE=22,//注意都是十进制
    AI_TYPE=19,
    DO_TYPE=24,
    DI_TYPE=23
};
class AO 
{
public: AO() 
    {
        wbuf[0] = 0x0E; // add   卡件位于十四
        wbuf[1] = 0x15; // length 21
        wbuf[2] = 0x01; // cmd
        wbuf[3] = 0x16; // typedevice 22

        wbuf[4] = 0x01;
        wbuf[5] = 0x01;
        wbuf[6] = 0x01;
        wbuf[7] = 0x01;
        wbuf[8] = 0x01;
        wbuf[9] = 0x01;
        wbuf[10] = 0x01;
        wbuf[11] = 0x01;
        wbuf[12] = 0x01;
        wbuf[13] = 0x01;
        wbuf[14] = 0x01;
        wbuf[15] = 0x01;
        wbuf[16] = 0x01;
        wbuf[17] = 0x01;
        wbuf[18] = 0x01;
        wbuf[19] = 0x01;
        wbuf[20] = XORCheck(wbuf, 20); // check
    }
       unsigned char wbuf[50];

};
class AI
{
public: AI() {
        wbuf[0] = 0x0F;              // add  卡件位于15
        wbuf[1] = 0x09;              // length 9
        wbuf[2] = 0x01;              // cmd
        wbuf[3] = 0x13;              // typedevice 19
        wbuf[4] = 0x01;              // add
        wbuf[5] = 0x01;              // length
        wbuf[6] = 0x01;              // cmd
        wbuf[7] = 0x01;              // typedevice
        wbuf[8] = XORCheck(wbuf, 8); // check
    }
      unsigned char  wbuf[50];

};
class DI
{
public: DI()
    {
        wbuf[0] = 0x31;              // add 卡件位于49
        wbuf[1] = 0x05;              // length 5
        wbuf[2] = 0x01;              // cmd
        wbuf[3] = 0x17;              // typedevice
        wbuf[4] = XORCheck(wbuf, 4); // check
    }
      unsigned char  wbuf[50];

};
class DO
{
public: DO()
    {
        wbuf[0] = 0x30; // add   卡件位于 48
        wbuf[1] = 0x09; // length
        wbuf[2] = 0x01; // cmd
        wbuf[3] = 0x18; // typedevice

        wbuf[4] = 0x01;
        wbuf[5] = 0x01;
        wbuf[6] = 0x01;
        wbuf[7] = 0x01;
        wbuf[8] = XORCheck(wbuf, 8); // check
    }
      unsigned char  wbuf[50];

};

class card
{
public:	void Init(int add,int type)
	{
        add_ = add;
        type_ = type;
        wbuf_[0] = add_;
        if (type_==AO_TYPE)
        {
            length_ = 21;
            for (int i=1;i<50;i++)
            {
                wbuf_[i] = ao.wbuf[i];
            }
        }
        if (type == AI_TYPE)
        {
            length_ = 9;
            for (int i = 1; i < 50; i++)
            {
                wbuf_[i] = ai.wbuf[i];
            }
        }
        if (type == DO_TYPE)
        {
            length_ = 9;
            for (int i = 1; i < 50; i++)
            {
                wbuf_[i] = d_o.wbuf[i];
            }
        }
        if (type == DI_TYPE)            
        {
			length_ = 5;
            for (int i = 1; i < 50; i++)
            {
                wbuf_[i] = di.wbuf[i];
            }
        }
	}
public:
	int add_;
	int type_;
    int length_;
	int wbuf_[50];
    AO ao;
    AI ai;
    DO d_o;
    DI di;
};
////AO 14 AI 15 
////DO 48 DI 49 
static std::vector<card>card_vector;
void Init(std::vector<card> &v)
{
   card c1;
   c1.Init(14, AO_TYPE);
   v.push_back(c1);

   card c2;
   c2.Init(15,AI_TYPE);
   v.push_back(c2);

   card c3;
   c3.Init(48,DO_TYPE);
   v.push_back(c3);

    card c4;
    c4.Init(49, DI_TYPE);
    v.push_back(c4);
};
class Card_Assemble
{
    public:
        Card_Assemble()
        {
            Init(card_vector_);
            size_=card_vector_.size();
            pthread_mutex_init(&mutex_, NULL);
        }
    private:
    int size_;
    std::vector<card>card_vector_;
    pthread_mutex_t mutex_;
    bool check[20];
};